package com.yyfax.pay.depository.controller.api;

import javax.annotation.Resource;
import javax.validation.Valid;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.yyfax.framework.common.OpResponse;
import com.yyfax.pay.common.annotation.CheckSign;
import com.yyfax.pay.common.enums.FuiouDepositStateEnum;
import com.yyfax.pay.common.exception.PayExceptionCode;
import com.yyfax.pay.common.model.to.YYPayApiReq2;
import com.yyfax.pay.depository.domain.FuiouTransferMerchantUser;
import com.yyfax.pay.depository.domain.FuiouTransferUserUser;
import com.yyfax.pay.depository.service.inner.FuiouTransferService;
import com.yyfax.pay.service.depository.AppropriatePreFrozenService;
import com.yyfax.pay.service.depository.FrozenService;
import com.yyfax.pay.service.depository.FrozenToFrozenService;
import com.yyfax.pay.service.depository.TransferPreFrozenService;
import com.yyfax.pay.service.depository.UnfrozenService;
import com.yyfax.pay.vo.depository.AppropriatePreFrozenReq;
import com.yyfax.pay.vo.depository.FrozenReq;
import com.yyfax.pay.vo.depository.FrozenToFrozenReq;
import com.yyfax.pay.vo.depository.RspContent;
import com.yyfax.pay.vo.depository.TradeResultRsp;
import com.yyfax.pay.vo.depository.TradeRsp;
import com.yyfax.pay.vo.depository.TransferPreFrozenReq;
import com.yyfax.pay.vo.depository.TransferReq;
import com.yyfax.pay.vo.depository.UnfrozenReq;

/**
 * 交易接口
 * 
 * @author heizq
 * @date 2017-09-02 18:22:48
 * @since v1.0.0
 */
@RestController
public class TradeApiController {

	@Resource
	private FrozenService frozenService;

	@Resource
	private TransferPreFrozenService transferPreFrozenService;

	@Resource
	private AppropriatePreFrozenService ppropriatePreFrozenService;

	@Resource
	private FrozenToFrozenService frozenToFrozenService;

	@Resource
	private UnfrozenService unfrozenService;

	@Resource
	private FuiouTransferService transferService;

	/**
	 * 冻结
	 * 
	 * @author heizq
	 * @date 2017-09-02 18:24:02
	 * @since v1.0.0
	 * @param req
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(method = RequestMethod.POST, path = "/api/trade/fuiou/frozen")
	@CheckSign
	public OpResponse frozen(@Valid @RequestBody YYPayApiReq2<FrozenReq> req) {
		OpResponse response = OpResponse.success();
		TradeRsp rsp = frozenService.frozen(req.getParams(), req.getMerchantCode());
		response.setCode(rsp.getCode());
		response.setDisplay(rsp.getDisplay());
		response.setContent(new TradeResultRsp(rsp.getBusiId()));
		return response;
	}

	/**
	 * 转账预冻结
	 * 
	 * @author heizq
	 * @date 2017-09-02 18:24:02
	 * @since v1.0.0
	 * @param req
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(method = RequestMethod.POST, path = "/api/trade/fuiou/transfer_frozen")
	@CheckSign
	public OpResponse transferFrozen(@Valid @RequestBody YYPayApiReq2<TransferPreFrozenReq> req) {
		OpResponse response = OpResponse.success();
		TradeRsp rsp = transferPreFrozenService.transferFrozen(req.getParams(), req.getMerchantCode());
		response.setCode(rsp.getCode());
		response.setDisplay(rsp.getDisplay());
		response.setContent(new TradeResultRsp(rsp.getBusiId()));
		return response;
	}

	/**
	 * 划拨预冻结
	 * 
	 * @author heizq
	 * @date 2017-09-02 18:24:02
	 * @since v1.0.0
	 * @param req
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(method = RequestMethod.POST, path = "/api/trade/fuiou/appropriate_frozen")
	@CheckSign
	public OpResponse appropriateFrozen(@Valid @RequestBody YYPayApiReq2<AppropriatePreFrozenReq> req) {
		OpResponse response = OpResponse.success();
		TradeRsp rsp = ppropriatePreFrozenService.appropriateFrozen(req.getParams(), req.getMerchantCode());
		response.setCode(rsp.getCode());
		response.setDisplay(rsp.getDisplay());
		response.setContent(new TradeResultRsp(rsp.getBusiId()));
		return response;
	}

	/**
	 * 冻结到冻结
	 * 
	 * @author heizq
	 * @date 2017-09-02 18:24:02
	 * @since v1.0.0
	 * @param req
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(method = RequestMethod.POST, path = "/api/trade/fuiou/frozen_frozen")
	@CheckSign
	public OpResponse frozenToFrozen(@Valid @RequestBody YYPayApiReq2<FrozenToFrozenReq> req) {
		OpResponse response = OpResponse.success();
		TradeRsp rsp = frozenToFrozenService.frozenToFrozen(req.getParams(), req.getMerchantCode());
		response.setCode(rsp.getCode());
		response.setDisplay(rsp.getDisplay());
		response.setContent(new TradeResultRsp(rsp.getBusiId()));
		return response;
	}

	/**
	 * 解冻
	 * 
	 * @author heizq
	 * @date 2017-09-02 18:24:02
	 * @since v1.0.0
	 * @param req
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(method = RequestMethod.POST, path = "/api/trade/fuiou/unfrozen")
	@CheckSign
	public OpResponse unfrozen(@Valid @RequestBody YYPayApiReq2<UnfrozenReq> req) {
		OpResponse response = OpResponse.success();
		TradeRsp rsp = unfrozenService.unfrozen(req.getParams(), req.getMerchantCode());
		response.setCode(rsp.getCode());
		response.setDisplay(rsp.getDisplay());
		response.setContent(new TradeResultRsp(rsp.getBusiId()));
		return response;
	}

	/**
	 * 转账(商户与个人之间)
	 *
	 * @author yejc
	 * @date 2017-09-05
	 * @since v2.0.0
	 * @param req
	 * @return
	 */
	@PostMapping("/api/transfer/merchant_and_user")
	@CheckSign
	public OpResponse merchantAndUser(@RequestBody @Valid YYPayApiReq2<TransferReq> req) {
		FuiouTransferMerchantUser order = transferService.transferMerchantAndUser(req.getParams(), req.getMerchantCode());
		OpResponse response = new OpResponse();
		if (order.getState().equals(FuiouDepositStateEnum.DEALING.getValue())) {
			response.setCode(PayExceptionCode.PROCESSING.getCode());
			response.setDisplay(PayExceptionCode.PROCESSING.getDesc());
		} else {
			response.setCode(order.getRespCode());
			response.setDisplay(order.getRespDesc());
			response.setContent(new RspContent(order.getBusiId()));
		}
		return response;
	}

	/**
	 * 划拨(个人与个人之间)
	 *
	 * @author yejc
	 * @date 2017-09-05
	 * @since v2.0.0
	 * @param req
	 * @return
	 */
	@PostMapping("/api/transfer/user_and_user")
	@CheckSign
	public OpResponse userAndUser(@RequestBody @Valid YYPayApiReq2<TransferReq> req) {
		FuiouTransferUserUser order = transferService.transferUserAndUser(req.getParams(), req.getMerchantCode());
		OpResponse response = new OpResponse();
		if (order.getState().equals(FuiouDepositStateEnum.DEALING.getValue())) {
			response.setCode(PayExceptionCode.PROCESSING.getCode());
			response.setDisplay(PayExceptionCode.PROCESSING.getDesc());
		} else {
			response.setCode(order.getRespCode());
			response.setDisplay(order.getRespDesc());
			response.setContent(new RspContent(order.getBusiId()));
		}
		return response;
	}
}
